#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "./lib/network_handler.h"
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    struct net_package package;


    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }

    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) 
        error("ERROR opening socket");

    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    // bzero((char *) &serv_addr, sizeof(serv_addr));
    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
   
/*======================================================================================================*/
// All above code needs to be fixed and cleansed 							 /
// Code just below is for testing									 /
/*======================================================================================================*/

	printf("Connected with socket descriptor: %d \n", sockfd); 
	printf("You are now connected with the server\n");

	printf("Trying to send a homemade package\n");

	package.buffer = (char *) malloc(sizeof("Dette er en streng\0"));
	package.size = sizeof(package.buffer);
	package.status = NET_READY2WRITE;

	SendPackage(sockfd, &package);	
	
	while(1)
	{
    		printf("Please enter the message: ");
   	        memset(buffer, 0, 256);
		fgets(buffer,255,stdin);

		// Handle input
		

	// =================================================================================== //
	//		Create seperate functions to handle writing and recieving data        //
	// 		it's needed for controlling the ddata flow of client & server	      //
	// ==================================================================================//			
		 RemoveNewLine(buffer);

		 if (strcmp(buffer, "quit") == 0)
		 {
			 SendMessage(sockfd, buffer);
	   		 if (RecieveMessage(sockfd, buffer) == 1)
				printf("%s", buffer);
		 }
	  	else if(strcmp(buffer, "sendfile")==0)
		{
			SendMessage(sockfd, buffer);
			// load file and start sending
			// Wait for OK 
			RecieveMessage(sockfd, buffer);

			// Laod data and start sending
			char *dataBuffer = (char *) malloc(2048);
			*dataBuffer = GetFile("./empty-file", dataBuffer, sizeof(dataBuffer));
			SendMessage(sockfd, dataBuffer);

		}
		else
		{
			SendMessage(sockfd, buffer);
		}
	 	 memset(buffer, 0, 256);
		 int i = sizeof(buffer);
		 for (i; i != 0; i--) buffer[i] = '\0';
		// This should clear up buffer, but it doesn't ! check todo
	} 




   n = read(sockfd,buffer,255);

    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}
